package com.zhiyume.service;

import com.zhiyume.dao.api.TCouponMapper;
import com.zhiyume.dao.entity.TCoupon;
import com.zhiyume.dao.entity.TCouponCriteria;
import com.zhiyume.dao.entity.TCoupon;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wang
 * @version $Id: TCouponService.java, v 0.1 2018-04-10 17:19:40 wang Exp $$
 */

@Service
public class TCouponService {
    @Autowired
    private TCouponMapper tCouponMapper;

    /**
     * 根据Id查询信息
     * @return
     */
    public TCoupon findCouponById(long id){
        TCouponCriteria example = new TCouponCriteria();
        TCouponCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TCoupon> couponList = tCouponMapper.selectByExample(example);
        if(null != couponList && couponList.size() > 0) {
            return couponList.get(0);
        }

        return new TCoupon();
    }


    /**
     * 添加信息
     * @param tCoupon     * @return
     */
    public TCoupon addCoupon(TCoupon tCoupon) throws Exception {

        long id = GenerateKeyUtil.generationPK();
        tCoupon.setId(id);
        tCoupon.setCreateTime(DateUtil.getNowFormatYYYYMDD());
        tCoupon.setUpdateTime(DateUtil.getNowFormatYYYYMDD());

        tCouponMapper.insert(tCoupon);

        return tCoupon;
    }
    
    
    /**
     * 修改人员信息
     * @param tCoupon     * @return
     */
    public int updateCoupon(TCoupon tCoupon) throws Exception{

        TCouponCriteria example = new TCouponCriteria();
        TCouponCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tCoupon.getId());

        return tCouponMapper.updateByExample(tCoupon,example);
    }
    
     /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int deleteCoupon(long id) throws Exception{
        TCouponCriteria example = new TCouponCriteria();
        TCouponCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);

        return tCouponMapper.deleteByExample(example);
    }


    public List<TCoupon> queryCouponList(TCoupon dto,PageReq page) {
        TCouponCriteria tCouponCriteria = new TCouponCriteria();
        TCouponCriteria.Criteria criteria = tCouponCriteria.createCriteria();
        tCouponCriteria.setLimitStart(page.getPage()*page.getPageSize());
        tCouponCriteria.setLimitEnd(page.getPageSize()*page.getPageSize()+page.getPageSize());

        createCriteria(dto, criteria);
        return tCouponMapper.selectByExample(tCouponCriteria);
    }



    public int countCoupon(TCoupon dto) {
        TCouponCriteria tCouponCriteria = new TCouponCriteria();
        TCouponCriteria.Criteria criteria = tCouponCriteria.createCriteria();

        createCriteria(dto, criteria);
        return tCouponMapper.countByExample(tCouponCriteria);
    }
    
    private void createCriteria(TCoupon dto, TCouponCriteria.Criteria criteria) {
        if(dto.getcId()!=null&&-1!=dto.getcId()){
            criteria.andCIdEqualTo(dto.getcId());
        }
        if(dto.getId()!=null&&-1!=dto.getId()){
            criteria.andIdEqualTo(dto.getId());
        }
        if(StringUtil.isNotBlank(dto.getTitle())){
            criteria.andTitleLike("%"+dto.getTitle()+"%");
        }
        if(StringUtil.isNotBlank(dto.getSerialNum())){
            criteria.andSerialNumEqualTo(dto.getSerialNum());
        }
        if(StringUtil.isNotBlank(dto.gettName())){
            criteria.andTNameLike("%"+dto.gettName()+"%");
        }
        if(StringUtil.isNotBlank(dto.getType())){
            criteria.andTypeLike("%"+dto.getType()+"%");
        }
        if(StringUtil.isNotBlank(dto.getIfUse())){
            criteria.andIfUseEqualTo(dto.getIfUse());
        }
        if(dto.getCreateId()!=null&&-1!=dto.getCreateId()){
            criteria.andCreateIdEqualTo(dto.getCreateId());
        }
        if(StringUtil.isNotBlank(dto.getCreateTime())){
            criteria.andCreateTimeLike("%"+dto.getCreateTime()+"%");
        }
        if(dto.getUpdateId()!=null&&-1!=dto.getUpdateId()){
            criteria.andUpdateIdEqualTo(dto.getUpdateId());
        }
        if(StringUtil.isNotBlank(dto.getUpdateTime())){
            criteria.andUpdateTimeLike("%"+dto.getUpdateTime()+"%");
        }
                
    }

}
